package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Array;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.ConfigUtil;

import com.alibaba.fastjson.JSONArray;
import com.util.BODY;
import com.util.HttpRequest;
import com.util.JSONUtil;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;

/**
 * 缴存历史记录
 * @author 
 */
@Controller
@RequestMapping("/jclsjlcontroller")
public class jclsjlcontroller{
   
	private static final Logger logger = Logger.getLogger(jclsjlcontroller.class);
	Map map  = new HashMap();
	List list = new ArrayList();
	  
	@RequestMapping("/test")
	@ResponseBody
	public String entryinterface(HttpServletRequest request, HttpServletResponse response ,HttpSession session, String limit ,String id,String start1,String end1,String UNITNAME,String sj) throws IOException{
		//读取配置文件路劲
		String sj1[]=sj.split(" - ");
		if(UNITNAME!=null)UNITNAME=new String(UNITNAME.getBytes("ISO-8859-1"),"UTF-8");
		if(sj1[0]!=null)start1=sj1[0].toString().replaceAll("-","");
		if(sj1[1]!=null)end1=sj1[1].toString().replaceAll("-", "");
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		Calendar a=Calendar.getInstance();
		Date e=new Date();
		String interfaceUrl = ReadConfig.readValue();
		start1+="01";
		end1+="31";
		Map reqMap = new HashMap();
		OpenKey opk=new OpenKey();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ109");
		//接口参数
		BODY b=new BODY();
		b.setGRZH(sessionIn.getGRZH());
		b.setQSRQ(start1);
		b.setZZRQ(end1);
		reqMap.put("BODY",b);
		reqMap.put("LOGUserName","admin");
		reqMap.put("OpenKey",sessionIn.getKEY());
		String result=opk.getOpenKey(reqMap, session);
		Map retMap = JSONUtil.toMap(result);
		
		logger.info("接口返回内容:"+result);
		if(result != null){
			Map resMap = JSONUtil.toMap(result);
			if(resMap.get("RETURNCODE").equals("100")){
				Map mx=JSONUtil.toMap(JSONUtil.ToJsongString2(resMap.get("body")));
				List l1=(List) mx.get("RETDATA");
				List li=new ArrayList();
				for(int i=0;i<l1.size();i++){
					Map l=(Map) l1.get(i);			
					if(start1==null||start1.equals(""))
						start1="0";
					if(end1==null||end1.equals(""))
						end1=new SimpleDateFormat("yyyyMMdd").format(new Date());
					if(l.get("SHRQ")==null)
							continue;
						else if(!(Integer.parseInt(l.get("SHRQ").toString())>=Integer.parseInt(start1)&&(Integer.parseInt(l.get("SHRQ").toString())<=Integer.parseInt(end1))))
							continue;		
					if(UNITNAME!=null&&!UNITNAME.equals("")){
					if(l.get("DWMC")==null)
						continue;
					else if(!l.get("DWMC").equals(UNITNAME))
						continue;
					}
					li.add(l);
				}
				map.put("code", 0); 
				map.put("msg", null);
				map.put("data",  li);
				list.add(resMap);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}else{
				map.put("code", 1);
				map.put("CW", resMap.get("RETURNDESC").toString());
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}
		}
		return null;
	}
}
